package org.px.gui;

import org.px.domain.UserIdentity;
import org.px.service.LeaveRecordService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class ApproveLeaveFrame extends JFrame {
    private UserIdentity currentUser;
    private JTextField leaveIdField;
    private JComboBox<String> statusCombo;
    private LeaveRecordService leaveRecordService = new LeaveRecordService();

    public ApproveLeaveFrame(JFrame parent, UserIdentity user) {
        this.currentUser = user;
        initUI();
        setLocationRelativeTo(parent);
    }


    private void initUI() {
        setTitle("员工请假管理系统 - 审批请假申请");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 请假ID标签和输入框
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(new JLabel("请假ID:"), gbc);

        gbc.gridx = 1;
        leaveIdField = new JTextField(25);
        panel.add(leaveIdField, gbc);

        // 审批状态标签和下拉框
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(new JLabel("审批状态:"), gbc);

        gbc.gridx = 1;
        statusCombo = new JComboBox<>(new String[]{"通过", "拒绝"});
        panel.add(statusCombo, gbc);

        // 审批按钮
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        JButton approveBtn = new JButton("审批");
        approveBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                approveLeaveApplication(); // 移除参数传递
            }
        });
        panel.add(approveBtn, gbc);

        add(panel);
    }

    private void approveLeaveApplication() {
        String leaveIdStr = leaveIdField.getText().trim();
        String status = (String) statusCombo.getSelectedItem();
        System.out.println("status: " + status);

        if (leaveIdStr.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请假ID不能为空", "审批错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            int leaveId = Integer.parseInt(leaveIdStr);
            String newStatus = "通过".equals(status) ? "已批准" : "已拒绝";
            System.out.println("newStatus: " + newStatus);

            // 使用成员变量获取审批人ID
            if (leaveRecordService.updateLeaveRecordStatus(leaveId, newStatus, currentUser.getUser_id())) {
                JOptionPane.showMessageDialog(this, "审批成功", "提示", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "审批失败，请重试", "审批错误", JOptionPane.ERROR_MESSAGE);
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "请假ID必须为数字", "审批错误", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "数据库操作异常：" + ex.getMessage(), "系统错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}